import { login, logout, getInfo } from '@/api/user'
import { getToken, setToken, removeToken } from '@/utils/auth'
import { resetRouter } from '@/router'
const state = {
    token: getToken(),
    // name: '',
    user: {},
    // introduction: '',
    roles: []
}
const mutations = {
    SET_TOKEN: (state, token) => {
        state.token = token
    },
    // SET_NAME: (state, name) => {
    //   state.name = name
    // },
    SET_AVATAR: (state, avatar) => {
        state.avatar = avatar
    },
    // SET_INTRODUCTION: (state, introduction) => {
    //   state.introduction = introduction
    // },
    SET_ROLES: (state, roles) => {
        state.roles = roles
    },
    SET_USER: (state, user) => {
        state.user = user
    }
}

const actions = {
    // user login
    Login({
        commit
    }, userInfo) {
        // const { username, password } = userInfo
        return new Promise((resolve, reject) => {
            login(userInfo).then(response => {
                const {
                    data
                } = response
                commit('SET_TOKEN', data.token)
                setToken(data.token)
                resolve(response)
            }).catch(error => {
                reject(error)
            })
        })
    },

    // get user info
    GetInfo({
        commit,
        state
    }) {
        return new Promise((resolve, reject) => {
            getInfo(state.token).then(response => {
                const {
                    data
                } = response

                if (data.roles && data.roles.length > 0) { // 验证返回的roles是否是一个非空数组
                    commit('SET_ROLES', data.roles)
                } else {
                    reject('没有获取到角色 !')
                }
                // const { name, avatar } = data
                commit('SET_USER', data.user)
                // commit('SET_NAME', name)
                // commit('SET_AVATAR', avatar)
                commit('SET_AVATAR', data.user.icon)
                resolve(data)
            }).catch(error => {
                reject(error)
            })
        })
    },

    // user logout
    Logout({
        commit,
        state
    }) {
        return new Promise((resolve, reject) => {
            logout(state.token).then(() => {
                // commit('SET_TOKEN', '')
                // removeToken()
                // resetRouter()
                // resolve()
                commit('SET_TOKEN', '')
                commit('SET_ROLES', [])
                /*
                commit('SET_MENUS', [])
                commit('SET_MENUPATHS', '')*/
                commit('SET_USER', {})
                removeToken()
                //state.tagsView.visitedViews =[];
                //state.tagsView.cachedViews =[];
                resolve()
            }).catch(error => {
                reject(error)
            })
        })
    },

    // remove token
    resetToken({
        commit
    }) {
        return new Promise(resolve => {
            commit('SET_TOKEN', '')
            removeToken()
            resolve()
        })
    }
}

export default {
    namespaced: true,
    state,
    mutations,
    actions
}
